﻿using STT.Entities;
using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Profile.UserControlsGV
{
    public partial class UcThongTinCaNhan : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            LoadGV();
        }
        private void LoadGV()
        {
            Dtlttcn.DataSource = tb.GetDataTableById2("GiangVien_GetByMagv", Session["MaFgv"].ToString());
            Dtlttcn.DataBind();
        }
        protected void Dtlttcn_CancelCommand(object source, DataListCommandEventArgs e)
        {
            Dtlttcn.EditItemIndex = -1;
            LoadGV();
        }

        protected void Dtlttcn_DeleteCommand(object source, DataListCommandEventArgs e)
        {

        }

        protected void Dtlttcn_EditCommand(object source, DataListCommandEventArgs e)
        {
            Dtlttcn.EditItemIndex = e.Item.ItemIndex;
            LoadGV();
        }

        protected void Dtlttcn_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
            if (e.CommandName.Equals("Update"))
            {
                ETbGiangVien gv = new ETbGiangVien();
                gv.MaGV = Dtlttcn.DataKeys[e.Item.ItemIndex].ToString();
                TextBox txttengv = (TextBox)e.Item.FindControl("txttengv");
                TextBox txtdiachi = (TextBox)e.Item.FindControl("txtdiachi");
                TextBox txtsdt = (TextBox)e.Item.FindControl("txtsdt");
                TextBox txtghichu = (TextBox)e.Item.FindControl("txtghichu");
                TextBox txtanh = (TextBox)e.Item.FindControl("txtanh");
                FileUpload fileup = (FileUpload)e.Item.FindControl("Fulanh");
                if (fileup.HasFile)
                {
                        if (System.IO.File.Exists(Server.MapPath("~/Uploads/Avatar/") + txtanh.Text))
                            System.IO.File.Delete(Server.MapPath("~/Uploads/Avatar/") + txtanh.Text);
                        string id = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString() + "¿" + fileup.FileName;
                        fileup.SaveAs(Server.MapPath("~/Uploads/Avatar/") + id);
                        gv.Anh = id;
                        //fileup.PostedFile.FileName.ToString();
                        gv.MaGV = Dtlttcn.DataKeys[e.Item.ItemIndex].ToString();
                        gv.TenGV = txttengv.Text;
                        gv.DiaChi = txtdiachi.Text;
                        gv.Sdt = txtsdt.Text;
                        gv.GhiChu = txtghichu.Text;
                        Business.BTbGiangVien.UpdateInfoGV(gv);
                }
                else
                {
                    gv.MaGV = Dtlttcn.DataKeys[e.Item.ItemIndex].ToString();
                    gv.TenGV = txttengv.Text;
                    gv.DiaChi = txtdiachi.Text;
                    gv.Sdt = txtsdt.Text;
                    gv.GhiChu = txtghichu.Text;
                    gv.Anh = txtanh.Text;
                    Business.BTbGiangVien.UpdateInfoGV(gv);
                }
                Dtlttcn.EditItemIndex = -1;
                Response.Redirect("Default.aspx?mod=TTCNGV117");
            }
            }
            catch { }
        }

        protected void Dtlttcn_ItemDataBound(object sender, DataListItemEventArgs e)
        {

        }

        protected void ddlHocHam_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlHocVi_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}